package person.bing.fbcloud.leetcode;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * @author: mengfanbing
 * @date: 2020/10/21 10:17
 */
public class Q925 {
    public static void main(String[] args) {
        boolean longPressedName = isLongPressedName("alex", "aaleex");
        System.out.println(longPressedName);
    }


    public static boolean isLongPressedName(String name, String typed) {
        char[] chars = name.toCharArray();
        int length = chars.length;
        StringBuilder sb = new StringBuilder();
        for (int i=0;i<length;i++){
            sb.append(chars[i]);
            if (i<length-1){
                if (chars[i] != chars[i+1]){
                    sb.append("+");
                }
            }
        }
        sb.append("+");
        boolean matches = Pattern.matches(sb.toString(), typed);
        return matches;
    }

    /**
     * 方法1
     * @param name
     * @param typed
     * @return
     */
    public static boolean isLongPressedName1(String name, String typed) {
        if(name == null){
            return true;
        }
        if (name != null && typed == null){
            return false;
        }
        char[] nameChars = name.toCharArray();
        char[] typedChars = typed.toCharArray();
        List<Character> nameNarrow = new ArrayList<>();
        List<Integer> nameCharSize = new ArrayList<>();
        List<Character> typedNarrow = new ArrayList<>();
        List<Integer> typedCharSize = new ArrayList<>();
        for (int i=0;i<nameChars.length;i++){
            if (i==0){
                nameNarrow.add(nameChars[0]);
                nameCharSize.add(1);
            }else {
                if (nameChars[i] == nameChars[i-1]){
                    nameCharSize.add(nameCharSize.remove(nameCharSize.size()-1)+1);
                }else {
                    nameNarrow.add(nameChars[i]);
                    nameCharSize.add(1);
                }
            }
        }
        for (int i=0;i<typedChars.length;i++){
            if (i==0){
                typedNarrow.add(typedChars[0]);
                typedCharSize.add(1);
            }else {
                if (typedChars[i] == typedChars[i-1]){
                    typedCharSize.add(typedCharSize.remove(typedCharSize.size()-1)+1);
                }else {
                    typedNarrow.add(typedChars[i]);
                    typedCharSize.add(1);
                }
            }
        }
        if (nameNarrow.toString().equals(typedNarrow.toString())){
            for (int i=0;i<nameCharSize.size();i++){
                if (nameCharSize.get(i).intValue() <= typedCharSize.get(i).intValue()){

                }else {
                    return false;
                }
                return true;
            }
        }
        return false;
    }
}
